package com.fingard.dsp.bank.directbank.hsbank01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @author: liuxb
 * @create: 2019-12-19 09:15
 * @version: 1.0
 **/
public class HSBANK01Balance extends HSBANK01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            WriteBankLogLn("进入余额查询处理类 ");
            while (p_req7506.readRecord()) {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //赋值通用参数
                HSBANK01Header bankHeader = new HSBANK01Header();
                bankHeader.trCode = "200108";
                bankHeader.reqNo = p_req7506.reqHeader.transSeqID;
//                bankHeader.serialNo = p_req7506.reqHeader.transSeqID;
                bankHeader.fileFlag = "0";//文件标识 0-报文 1-文件
                //写入HSBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                XElement tmpXElement = new XElement("body", "");
                tmpXElement.addChild(new XElement("acno", p_req7506.readedDetail.actNum));
                tmpXElement.addChild(new XElement("cur_code", convertCurToBank(p_req7506.readedDetail.actCur)));
                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndDocument();

                //模拟银行返回
//            String s = "<ap>\n" +
//                    "\t\t<head>\n" +
//                    "\t\t\t<tr_code>2323</tr_code>\n" +
//                    "<cms_corp_no>1515</cms_corp_no>\n" +
//                    "<user_no>7878</user_no>\n" +
//                    "<org_code>1414</org_code>\n" +
//                    "<serial_no>8989</serial_no>\n" +
//                    "<req_no>6767</req_no>\n" +
//                    "<tr_acdt>20191223</tr_acdt>\n" +
//                    "<tr_time>164130</tr_time>\n" +
//                    "<succ_flag>0</succ_flag>\n" +
//                    "<ret_code>0000</ret_code>\n" +
//                    "<ret_info>成功！</ret_info>\n" +
//                    "<ext_info>返回附加信息</ext_info>\n" +
//                    "<file_flag>文件标识</file_flag>\n" +
//                    "<reserved>保留字段</reserved>\n" +
//                    "\t\t</head>\n" +
//                    "\t\t<body>\n" +
//                    "\t\t\t\t\t<acno>3434</acno>\n" +
//                    "\t\t\t\t\t<acname>账户名称</acname>\n" +
//                    "\t\t\t\t\t<balance>100000.32</balance>\n" +
//                    "\t\t\t\t\t<use_balance>90000.22</use_balance>\n" +
//                    "\t\t\t\t\t<freeze_bal>10000.10</freeze_bal>\n" +
//                    "\t\t</body>\n" +
//                    "</ap>\n";
//            String[] tmpRetBkStr = new String[]{"", s};

                String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());

                if (tmpRetBkStr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String returnCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                        String returnMsg = tmpBkReader.getFirstText("/ap/head/ret_info");
                        if (returnCode != null && "0000".equals(returnCode)) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = "0";
                            tmpRetDetail.respInfo = returnMsg;
//                            tmpRetDetail.actCur = p_req7506.readedDetail.actCur;//币种
                            tmpRetDetail.actNum = p_req7506.readedDetail.actNum;//银行账号
                            tmpRetDetail.actArea = p_req7506.readedDetail.actArea;//账号地区区域码

                            tmpRetDetail.currBal = tmpBkReader.getFirstText("/ap/body/balance");//余额
                            tmpRetDetail.availBal = tmpBkReader.getFirstText("/ap/body/use_balance");//可用余额
                            tmpRetDetail.frozenBal = tmpBkReader.getFirstText("/ap/body/freeze_bal");//冻结余额
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        } else if (returnCode != null && "9999".equals(returnCode)) {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo("银企直联通讯机校验错误，或者通讯失败！");
                            break;
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(returnMsg);
                            break;
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                        break;
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpRetBkStr[1]);
                    break;
                }
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
            WriteBankLogLn("余额查询异常：" + e);
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}

